<?php

namespace App\Console\Commands\Assets;

use App\Models\Assets\ExpensesByDayModel;
use App\Models\Assets\RevenuesByDayModel;
use App\Models\User;
use App\Service\Assets\AssetsService;
use Carbon\Carbon;
use Illuminate\Console\Command;

class SummaryTrend extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'assets:summary-trend';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每天汇总收益和支出';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        // 每天5点汇总昨日的
        $time = Carbon::now()->subDay();

        $userList = User::query()
            ->where('status', User::STATUS_ON)
            ->get();

        $userList->each(function ($user) use ($time) {
            // 汇总收益
            $revenueAmount                 = AssetsService::revenues($user->id, $time);
            $revenueByDayModel             = new RevenuesByDayModel;
            $revenueByDayModel->amount     = $revenueAmount;
            $revenueByDayModel->user_id    = $user->id;
            $revenueByDayModel->created_at = $time->timestamp;
            $revenueByDayModel->save();

            // 汇总支出
            $expenseAmount              = AssetsService::expenses($user->id, $time);
            $expenseByDayModel          = new ExpensesByDayModel;
            $expenseByDayModel->amount  = $expenseAmount;
            $expenseByDayModel->user_id = $user->id;
            $expenseByDayModel->created_at = $time->timestamp;
            $expenseByDayModel->save();
        });


    }

}
